package com.recharge.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.util.BeanUtilsExtends;
import com.recharge.common.util.Constant;
import com.recharge.core.entity.BlacklistEntity;
import com.recharge.core.entity.BlacklistEntity;
import com.recharge.core.entity.BlacklistRegionEntity;
import com.recharge.core.mapper.BlacklistMapper;
import com.recharge.core.mapper.BlacklistRegionMapper;
import com.recharge.domain.query.BlacklistQueryDO;
import com.recharge.domain.vo.BlacklistDO;
import com.recharge.domain.vo.BlacklistRegionDO;
import com.recharge.service.BlacklistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * 黑名单 服务实现类
 * </p>
 *
 * @author Reagan
 * @since 2020-09-21
 */
@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, BlacklistEntity> implements BlacklistService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private BlacklistRegionMapper blacklistRegionMapper;

    public ResultDO add(BlacklistDO domain) {

        BlacklistEntity entity = new BlacklistEntity();

        ResultDO result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        entity.setCreateDate(Calendar.getInstance().getTime());
        entity.setModifiDate(entity.getCreateDate());
        entity.setModifier(entity.getCreator());


        boolean res = false;
        try {
            res = this.save(entity);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.BLACKLIST_ADD_ERROR);
            logger.debug("error code：" + ErrorCode.BLACKLIST_ADD_ERROR.getCode() + " msg" + ErrorCode.BLACKLIST_ADD_ERROR.getMsg());
            return result;
        }

        if(domain.getBlacklistRegions() != null) {
            for(String regionCode : domain.getRegionCodes()) {
                BlacklistRegionEntity blacklistRegionEntity = new BlacklistRegionEntity();
                blacklistRegionEntity.setBlacklistId(entity.getId());
                blacklistRegionEntity.setRegionCode(regionCode);
                try {
                    blacklistRegionMapper.insert(blacklistRegionEntity);
                } catch(Exception e) {
                    result.setSuccess(false);
                    result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
                    logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
                    return result;
                }
            }
        }

        return result;
    }



    public ResultDO get(String id) {

        ResultDO result = new ResultSupport();
        BlacklistEntity entity = null;

        try {
            entity = this.getById(id);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        List<BlacklistRegionEntity> list = null;
        try {
            list = blacklistRegionMapper.selectList(Wrappers.<BlacklistRegionEntity>lambdaQuery().eq(BlacklistRegionEntity::getBlacklistId, id));
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }


        BlacklistDO domain = getDomain(entity);

        domain.setBlacklistRegions(getDomainBlacklistRegionList(list));

        result.setModel(ResultDO.FIRST_MODEL_KEY, domain);

        return result;
    }


    public ResultDO remove(String id) {
        ResultDO result = new ResultSupport();

        boolean res = false;
        try {
            res = this.removeById(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        try {
            blacklistRegionMapper.delete(Wrappers.<BlacklistRegionEntity>lambdaQuery().eq(BlacklistRegionEntity::getBlacklistId, id));
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.BLACKLIST_DELETE_ERROR);
            logger.debug("error code：" + ErrorCode.BLACKLIST_DELETE_ERROR.getCode() + " msg" + ErrorCode.BLACKLIST_DELETE_ERROR.getMsg());
            return result;
        }

        return result;
    }


    public ResultDO find(BlacklistQueryDO query) {
        return null;
    }

    private BlacklistDO getDomain(BlacklistEntity entity) {

        //判断房源对象是否为空，为空就返回 空，不为空就进行下面的代码
        if(entity == null) return null;
        BlacklistDO domain = new BlacklistDO();

        domain = BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;

        //判断拷贝数据到Dto对象是否成功，如果成功则返回domian，如果不成功就返回空
        return domain;
    }

    private List<BlacklistDO> getDomainList(List<BlacklistEntity> list) {
        //创建一个名为dolist的房源do类型的 list集合
        List<BlacklistDO> doList = new ArrayList<BlacklistDO>();

        //判断传入的list的集合的size是否大于0
        if(list.size() > 0) {
            //大于0是，遍历list，创建do类型的对象 调用getdomain方法
            for(BlacklistEntity entity : list) {

                BlacklistDO domain = this.getDomain(entity);

                //判断得到的domain 是否为空 不为空 就往dolist集合里面添加domian 为空就返回空
                if(domain == null) {
                    return null;
                }
                doList.add(domain);
            }
        } else {
            return null;
        }
        return doList;
    }

    private BlacklistRegionDO getDomain(BlacklistRegionEntity entity) {
        ResultDO result = new ResultSupport();
        //判断房源对象是否为空，为空就返回 空，不为空就进行下面的代码
        if(entity == null) return null;
        BlacklistRegionDO domain = new BlacklistRegionDO();

        if(!result.isSuccess()) {
            return null;
        }
        domain = BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;

        //判断拷贝数据到Dto对象是否成功，如果成功则返回domian，如果不成功就返回空
        return domain;
    }

    private List<BlacklistRegionDO> getDomainBlacklistRegionList(List<BlacklistRegionEntity> list) {
        //创建一个名为dolist的房源do类型的 list集合
        List<BlacklistRegionDO> doList = new ArrayList<BlacklistRegionDO>();

        //判断传入的list的集合的size是否大于0
        if(list.size() > 0) {
            //大于0是，遍历list，创建do类型的对象 调用getdomain方法
            for(BlacklistRegionEntity entity : list) {

                BlacklistRegionDO domain = this.getDomain(entity);

                //判断得到的domain 是否为空 不为空 就往dolist集合里面添加domian 为空就返回空
                if(domain == null) {
                    return null;
                }
                doList.add(domain);
            }
        } else {
            return null;
        }
        return doList;
    }
}
